/**
 * <p>Title: ServiceZP14.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:46:23  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.es.domain.ESPost;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zp.ZPUtils;
import com.baosight.sczx.zp.domain.ZPUserset;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.iplat4j.security.cmd.CommandInvoker;
import com.baosight.iplat4j.security.hierarchy.cmd.CreateRole;
import com.baosight.iplat4j.security.hierarchy.cmd.DeleteRole;
import com.baosight.platform.core.security.base.GlobalConfig;
import com.baosight.platform.core.security.base.RoleTypeAlreadyExistException;

/**
 * 角色管理
 * <p>Title: ServiceZP14.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:46:23  张志芳  Create
 *</pre>
 */
public class ServiceZP14 extends ServiceEPBase {

	SecurityBridge bridge = SecurityBridgeFactory.getBridge();
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:00 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.set("orgId", ZPUtils.USER_COMPANY_ID);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:12 张志芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{
		StaticTools.setQueryParameters(inInfo, "orgLabel", ZPUtils.USER_COMPANY_ID, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZPUserset.query", new ZPUserset());
		return outInfo;
	}
	

	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:29 张志芳
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		String orgCode = inInfo.getCellStr("result", 0, "orgLabel");
		String roleLabel = inInfo.getCellStr("result", 0, "postTypeLabel");
		String roleName = inInfo.getCellStr("result", 0, "postTypeName");
	
		String project = FrameworkInfo.getProjectEname();
		try{
			bridge.createRoleType(project, roleLabel, roleName);
		}catch( RoleTypeAlreadyExistException e )
		{
			// 新建角色类型时，该角色已经存在，故不重新创建
			e.printStackTrace(); 
			inInfo.setStatus(-1);
			inInfo.setMsg("该角色已经存在！");
			return inInfo;
		}
		CreateRole cmd = new CreateRole(orgCode, roleLabel);
		CommandInvoker.invoke( cmd );
		
		//查询角色类型对应的角色
		String role = "";
		Map map=new HashMap();
		map.put("postTypeLabel", roleLabel);
		List list = dao.query("ZPUserset.queryRole", map);
		if(list.size()>0)
		{
			role=((ZPUserset)list.get(0)).getPostLabel();
		}
		
		//添加资源组
		if(inInfo.getBlock("resResult") != null){
			List resourcesets = inInfo.getBlock("resResult").getRows();
			for (int i = 0; i < resourcesets.size(); i++) {
				Map set = (Map) resourcesets.get(i);
				String setName = (String) set.get("Name");
				bridge.grantAccess(role, setName, GlobalConfig.TYPE_RESOURCESET);
			}
		}
		
		//添加人员
		if(inInfo.getBlock("userResult") != null){
			List members = inInfo.getBlock("userResult").getRows();
			for (int i = 0; i < members.size(); i++) {
				Map member = (Map) members.get(i);
				String userName = (String) member.get("loginName");
				bridge.addUserToRole(role, userName);
			}
		}
		return inInfo;
	}
	
	/**
	 * 修改
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:29 张志芳
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo)
	{
		String roleLabel = inInfo.getCellStr("result", 0, "postTypeLabel");
		String roleName = inInfo.getCellStr("result", 0, "postTypeName");
		
		bridge.updateRoleTypeDisplayName(roleLabel , roleName );
		return inInfo;
	}
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:46 张志芳
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo)
	{
		int total = inInfo.getBlock( EiConstant.resultBlock ).getRowCount();
		for (int i = 0; i < total; i++) {			
			String role = inInfo.getBlock( EiConstant.resultBlock ).getRow(i).get("postLabel").toString();
			
			DeleteRole cmd = new DeleteRole(role);
			CommandInvoker.invoke( cmd );								
		}
		
		return this.query(inInfo);
	}
	
}
